package tankgame03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;

/**
 * @author DCSGO
 * @version 1.0
 * 游戏绘图区
 */

@SuppressWarnings({"all"})
public class MyPanel extends JPanel implements KeyListener, Runnable {
    private boolean loop = true;
    PlayerTank playerTank;

    //上局游戏剩余敌方坦克位置相关信息
    ArrayList<Node> lastEnemyTanks = new ArrayList<>();

    //1.敌人数量可能很多 应选用集合来存储
    //2.以后会有多线程的需求 故选用 Vector
    private int enemyTanksSize;
    Vector<EnemyTank> enemyTanks = new Vector<>();

    //为实现爆炸效果 添加炸弹集合
    Vector<Bomb> bombs = new Vector<>();
    Image image1;
    Image image2;
    Image image3;

    @Override
    public void run() {
        while (loop) {
            //判断是否有敌方坦克被玩家击中并做出相应操作
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                for (int j = 0; j < playerTank.shots.size(); j++) {
                    Shot playerShot = playerTank.shots.get(j);
                    if (playerShot.visible && enemyTank.beHit(playerShot)) {
                        //玩家击中敌方坦克 炮弹消失
                        Recorder.bonusPoints();
                        playerShot.visible = false;
                        playerTank.shots.remove(playerShot);
                        //消除被毁坦克的炮弹
                        for (Shot shot : enemyTank.shots) {
                            shot.visible = false;
                        }
                        enemyTank.shots.clear();
                        //添加爆炸
                        bombs.add(new Bomb(enemyTank.getX(), enemyTank.getY()));
                        //终止敌方被毁坦克线程
                        enemyTank.isLive = false;
                        /*Vector<EnemyTank> others = (Vector<EnemyTank>) enemyTanks.clone();
                        others.remove(enemyTank);*/
                        enemyTanks.remove(enemyTank);

                    }
                }
            }

            //判断游戏结束
            if (enemyTanks.isEmpty()) {
                System.out.println("你消灭了所有敌方坦克 3s 后退出");
                loop = false;
                try {
                    Recorder.save();
                    LastEnemyTanks.save(enemyTanks);
                    Thread.sleep(3 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }

            if (playerTank.beHit(enemyTanks)) {
                for (int i = 0; i < playerTank.shots.size(); i++) {
                    Shot shot = playerTank.shots.get(i);
                    shot.visible = false;
                }
                playerTank.isLive = false;
                bombs.add(new Bomb(playerTank.getX(), playerTank.getY()));
                playerTank.setX(-1);
                playerTank.setY(-1);
                System.out.println("你被击毁了 3s 后退出");
                try {
                    Recorder.save();
                    LastEnemyTanks.save(enemyTanks);
                    Thread.sleep(3 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }

            repaint();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //游戏绘图区
        g.fillRect(0, 0, 1000, 750);

        //绘制玩家坦克
        if (playerTank.isLive) {
            drawTank(playerTank.getX(), playerTank.getY(), g, playerTank.getDirection(), 0);
        }

        //绘制爆炸效果
        if (!bombs.isEmpty()) {
            for (int i = 0; i < bombs.size(); i++) {
                Bomb bomb = bombs.get(i);
                if (bomb.visible) {
                    if (bomb.getState() == Thread.State.NEW) {
                        bomb.start();
                    }
                    if (bomb.life > 6) {
                        g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
                    } else if (bomb.life > 3) {
                        g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
                    } else {
                        g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
                    }
                } else {
                    bombs.remove(bomb);
                }
            }
        }

        //绘制玩家炮弹
        drawShell(playerTank.shots, g);

        //绘制敌方坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
        }

        //绘制敌方坦克的炮弹
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
                drawShell(enemyTank.shots, g);
            }
        }

        //绘制积分面板
        drawIntegral(g);
    }

    /**
     * @param x         坦克左上角x坐标
     * @param y         坦克左上角y坐标
     * @param g         能用于绘制坦克的图案对象
     * @param direction 坦克的方向(0-上 1-右 2-下 3-左)
     * @param type      坦克类型(0 玩家操控的坦克 1 敌人的坦克)
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        //设置颜色
        switch (type) {
            case 0://玩家操控的坦克
                g.setColor(Color.cyan);
                break;
            case 1://敌人的坦克
                g.setColor(Color.yellow);
                break;

            default:
                System.out.println("暂时没有该坦克类型");
                break;
        }
        //根据方向绘制坦克
        switch (direction) {
            case 0://上
                g.fill3DRect(x, y, 10, 60, false);//左履带
                g.fill3DRect(x + 30, y, 10, 60, false);//右履带
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//车体
                g.fillOval(x + 10, y + 20, 20, 20);//炮台
                g.drawLine(x + 20, y, x + 20, y + 20);//炮筒
                break;

            case 1://右
                g.fill3DRect(x, y, 60, 10, false);//左履带
                g.fill3DRect(x, y + 30, 60, 10, false);//右履带
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//车体
                g.fillOval(x + 20, y + 10, 20, 20);//炮台
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//炮筒
                break;

            case 2://下
                g.fill3DRect(x, y, 10, 60, false);//左履带
                g.fill3DRect(x + 30, y, 10, 60, false);//右履带
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//车体
                g.fillOval(x + 10, y + 20, 20, 20);//炮台
                g.drawLine(x + 20, y + 60, x + 20, y + 20);//炮筒
                break;

            case 3://左
                g.fill3DRect(x, y, 60, 10, false);//左履带
                g.fill3DRect(x, y + 30, 60, 10, false);//右履带
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//车体
                g.fillOval(x + 20, y + 10, 20, 20);//炮台
                g.drawLine(x + 30, y + 20, x, y + 20);//炮筒
                break;

            default:
                System.out.println("暂时没有该方向");
                break;
        }
    }

    /**
     * @param shots 需要绘制的所有炮弹
     * @param g     能用于绘制坦克的图案对象
     */
    public void drawShell(Vector<Shot> shots, Graphics g) {
        if (!shots.isEmpty()) {
            for (int i = 0; i < shots.size(); i++) {
                Shot shot = shots.get(i);
                if (shot.visible) {
                    g.fillOval(shot.x, shot.y, 3, 3);
                }
            }
        }
    }

    public void drawIntegral(Graphics g) {
        g.setColor(Color.BLACK);
        g.setFont(new Font("微软雅黑", Font.BOLD, 30));
        g.drawString("已消灭敌方坦克: ", 1000, 30);
        drawTank(1000, 40, g, 1, 1);
        g.setColor(Color.BLACK);
        g.drawString(Recorder.getIntegral() + "", 1000 + 60 + 20, 40 + 30);
    }

    //0开始新游戏 1继续上局游戏
    public MyPanel() {
        //初始化爆炸图片
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));

        playerTank = new PlayerTank(100, 100, 5);

        //恢复上局游戏
        lastEnemyTanks = LastEnemyTanks.loadLastEnemyTanks();
        if (!lastEnemyTanks.isEmpty()) {
            System.out.println("1继续上局游戏 0开始新游戏");
            System.out.println("请选择：");
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            if (i == 1) {
                for (int j = 0; j < lastEnemyTanks.size(); j++) {
                    Node node = lastEnemyTanks.get(j);
                    EnemyTank enemyTank = new EnemyTank(node.getX(), node.getY(), 3);
                    enemyTank.setDirection(node.getDirection());
                    enemyTanks.add(enemyTank);
                }
                //设置各个敌方坦克的 otherTanks
                for (int j = 0; j < lastEnemyTanks.size(); j++) {
                    EnemyTank enemyTank = enemyTanks.get(j);
                    enemyTank.otherTanks = (Vector<EnemyTank>) enemyTanks.clone();
                    enemyTank.otherTanks.remove(enemyTank);
                }

                new AePlayWave("src\\111.wav").start();
                return;
            }
        }

        enemyTanksSize = 8;
        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = new EnemyTank(60 * (i + 4), 60 * (i + 4), 3);
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
        }
        //设置各个敌方坦克的 otherTanks
        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            enemyTank.otherTanks = (Vector<EnemyTank>) enemyTanks.clone();
            enemyTank.otherTanks.remove(enemyTank);
        }
        //播放游戏音乐
        new AePlayWave("src\\111.wav").start();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override //响应键盘操作
    public void keyPressed(KeyEvent e) {
        if (playerTank.isLive) {
            if (e.getKeyCode() == KeyEvent.VK_W) {//w-上
                //向上移动
                playerTank.moveUp();
            } else if (e.getKeyCode() == KeyEvent.VK_D) {//d-右
                playerTank.moveRight();

            } else if (e.getKeyCode() == KeyEvent.VK_S) {//s-下
                playerTank.moveDown();

            } else if (e.getKeyCode() == KeyEvent.VK_A) {//a-左
                playerTank.moveLeft();

            } else if (e.getKeyCode() == KeyEvent.VK_J) {//j-发射炮弹

                playerTank.shotEnemy();

            }
            //让面板重绘 刷新
            repaint();
        }
    }


    @Override
    public void keyReleased(KeyEvent e) {

    }
}
